/*
 * AliasMapper.java
 *
 * Copyright 2011 Anish
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.aosp.nanoxstream;

import java.util.Hashtable;


/**
 * <code>AliasMapper.java</code>
 *  <p> Mapping Field alias and Type Alias.
 * A Field represents a field Decalred in Class, and alias is name in xml.
 * </p>
 * @author Anish , anishkumar.ka@gmail.com
 * <dt><b>Date: </b><dd>Jan 5, 2011
 * <dt><b>Module: </b><dd>nanoxstream
 * <dt><b>Package: </b><dd>com.aosp.nanoxstream
 * <dt>TODO:<dd>
 */
public class AliasMapper {
	
	/**
	 * this alias used for aliasing a field in the class with an element in the XML document.
	 */
	private Hashtable fieldAliases = null;
	
	/**
	 * this alias used for aliasing an element in the XML document with a field in the class.
	 */
	private Hashtable aliasForField = null;
	
	/**
	 * this alias used for aliasing a Class with an element in the XML document.
	 */
	private Hashtable aliasesToType = null;
	
	/**
	 * this alias used for aliasing an element in the XML document with a Class.
	 */
	private Hashtable typeToAlias = null;

	/**
	 * Constructor
	 */
	protected AliasMapper() {
		this.fieldAliases = new Hashtable();
		this.aliasForField = new Hashtable();
		this.aliasesToType = new Hashtable();
		this.typeToAlias = new Hashtable();
	}
	
	/**
	 * <pre> Aliases for Class </pre>
	 * @param fieldName <tt> alias appeared in the xml</tt>
	 * @param type <tt> Class type</tt>
	 */
	protected void alias(String fieldName,Class type){
		aliasesToType.put(fieldName, type);
		typeToAlias.put(type, fieldName);
	}
	
	/**
	 * <pre> Aliases for a field in the class.</pre>
	 * @param alias <tt> alias appeared in the xml</tt>
	 * @param definedIn <tt> Class type where the field is defined </tt>
	 * @param fieldName <tt> field name within the class </tt>
	 */
	protected void aliasField(String alias, Class definedIn, String fieldName){
		Hashtable aliasedFields = (Hashtable) fieldAliases.get(definedIn);
		Hashtable aliasForFieldProperty = (Hashtable) aliasForField.get(definedIn);
		if(aliasedFields!=null){
			aliasedFields.put(alias, fieldName);
		}else{
			aliasedFields = new Hashtable();
			aliasedFields.put(alias, fieldName);
			fieldAliases.put(definedIn,aliasedFields);
		}
		if(aliasForFieldProperty!=null){
			aliasForFieldProperty.put(fieldName , alias);
		}else{
			aliasForFieldProperty = new Hashtable();
			aliasForFieldProperty.put(fieldName , alias);
			aliasForField.put(definedIn,aliasForFieldProperty);
		}
	}

	/**
	 * For from xml
	 * @return aliases
	 */
	protected Hashtable getAliases() {
		return aliasesToType;
	}

	/**
	 * For to xml String
	 * @return
	 */
	protected Hashtable getTypeToAlias() {
		return typeToAlias;
	}
	
	
	/**
	 * <p> Returns the field aliases holding Hashtable</p>
	 * @return fieldAliases {@link Hashtable}
	 */
	protected Hashtable getFieldAliases() {
		return fieldAliases;
	}
	
	/**
	 * <p>Get the aliases for the specified class</p>
	 * @param definedIn <tt> Class type where the field is defined </tt>
	 * @return <tt> The Properties containing aliased Field </tt>
	 */
	protected Hashtable getFieldAliases(Class definedIn) {
		return (Hashtable) fieldAliases.get(definedIn);
	}
	
	/**
	 * <p>Get the aliased field</p>
	 * @param definedIn <tt> Class type where the field is defined </tt>
	 * @param alias <tt> alias appeared in the xml</tt>
	 * @return <tt> The aliased Field </tt>
	 */
	protected String getFieldAliases(Class definedIn,String alias) {
		return (((Hashtable) fieldAliases.get(definedIn))==null?null:((String)((Hashtable) fieldAliases.get(definedIn)).get(alias)));
	}
	
	/**
	 * <p>Get the aliase for a field, used for to xml string</p>
	 * @param definedIn <tt> Class type where the field is defined </tt>
	 * @param fieldName <tt> Field name appeared in the Class</tt>
	 * @return <tt> The aliase For the specified field.</tt>
	 */
	protected String getAliasForField(Class definedIn,String fieldName) {
		return (((Hashtable) aliasForField.get(definedIn))==null?null:((String)((Hashtable) aliasForField.get(definedIn)).get(fieldName)));
	}
}
